//
//  Semester.m
//  Gradekeeper0.2
//
//  Created by Tim Qian on 09-12-1.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "Semester.h"


@implementation Semester

@synthesize term;
@synthesize courses;
@synthesize year;
@synthesize credits;
@synthesize GPA;

-(BOOL) addCourse: (Courses*) newCourse {
	NSEnumerator *enumerator = [self.courses objectEnumerator];
    id obj;
	NSUInteger i = 0;
	
    while ( obj = [enumerator nextObject] ) {
        if ([obj getName] > [newCourse getName]) {
			[self.courses insertObject:newCourse atIndex:i];
			return YES;
		} else if ([obj getName] == [newCourse getName]) {
			return NO;
		}
		i++;
    }
	
	[self.courses addObject:newCourse];
	return YES;
}

-(void) update {
	int newCredits = 0;
	float sumGPA = 0;
	
    for (int i=0; i<[self.courses count]; i++) {
		newCredits += [[self.courses objectAtIndex:i] credit];
		sumGPA += [[self.courses objectAtIndex:i] gradePoint]*[[self.courses objectAtIndex:i] credit];
	}
	
	credits = newCredits;
	GPA = sumGPA/newCredits;
}
@end
